#include "Product.h"
#include <QtGui>
void Product::setName(QString name1)
{
    name=name1;
}

void Product::setWeight(int weight1)
{
    weight=weight1;
}

QString Product::getName()
{
    return name;
}
int Product::getWeight()
{
    return weight;
}

bool Product::inBase()
{
    bool t = false;
    QFile file("baseProduct.xml");
    if (file.open(QIODevice::ReadOnly))
    {
        if (docProd.setContent(&file))
        {
            QDomElement root = docProd.documentElement();
            QDomElement child = root.firstChildElement("product");
            qDebug()<<(child.attribute("name", ""));
            while ((!child.isNull()) && (child.attribute("name", "")!=name))
            {
                child = child.nextSiblingElement("product");
            }
            if (child.attribute("name", "")== name)
            {
                t=true;
            }
        }
        file.close();
    }

    return t;

}

bool Product::setPar()
{
    bool t=true;
    QFile file("baseProduct.xml");
    if (file.open(QIODevice::ReadOnly))
    {
        if (docProd.setContent(&file))
        {
            QDomElement root = docProd.documentElement();
            QDomElement child = root.firstChildElement("product");
            qDebug()<<(child.attribute("name", ""));
            while ((!child.isNull()) && (child.attribute("name", "")!=name))
            {
                child = child.nextSiblingElement("product");
                qDebug()<<(child.attribute("name", ""));
            }
            if (child.attribute("name", "")== name)
            {
                Protein = child.firstChildElement("protein").text().toDouble()*getWeight()/100;
                Fat=child.firstChildElement("fat").text().toDouble()*getWeight()/100;
                Carbohydrate = child.firstChildElement("carbohydrate").text().toDouble()*getWeight()/100;
                Energy = child.firstChildElement("energy").text().toDouble()*getWeight()/100;
                /* Fat=child.attribute("fat", "").toDouble()*getWeight()/100;
                 Protein = child.attribute("protein", "").toDouble()*getWeight()/100;
                Carbohydrate = child.attribute("carbohydrate", "").toDouble()*getWeight()/100;
                Energy = child.attribute("energy", "").toDouble()*getWeight()/100;*/
            }
            else
            {
                t=false;
                //qDebug()<<"Not founded!";
                }
}
           else
            {

               qDebug()<<"No setContent!";
            }
            file.close();
            }
    return t;
}

double Product::getCarbohydrate()
{
    return Carbohydrate;
}

double Product::getEnergy()
{
    return Energy;
}

double Product::getFat()
{
    return Fat;
}

double Product::getProtein()
{
    return Protein;
}

void Product::setCarbohydrate(double carbohydrate)
{
    Carbohydrate=carbohydrate;
}

void Product::setEnergy(double energy)
{
    Energy=energy;
}

void Product::setFat(double fat)
{
    Fat=fat;
}

void Product::setProtein(double protein)
{
    Protein=protein;
}

void Product::addProduct()
{
    QFile file("baseProduct.xml");
    QFile file1 ("baseProd.xml");
    if (file.open(QIODevice::ReadWrite))
    {
        if (docProd.setContent(&file))
        {
            qDebug()<<"In added!";

            const int Indent = 4;
            QDomElement root = docProd.documentElement();
            QDomElement product = docProd.createElement("product");
            product.setAttribute("name",name);
            QDomElement protein = docProd.createElement("protein");
            QDomElement fat = docProd.createElement("fat");
            QDomElement carbohydrate = docProd.createElement("carbohydrate");
            QDomElement energy = docProd.createElement("energy");
            QDomText fatText = docProd.createTextNode(QString::number(Fat));
            QDomText carbohydrateText = docProd.createTextNode(QString::number(Carbohydrate));
            QDomText proteinText = docProd.createTextNode(QString::number(Protein));
            QDomText energyText = docProd.createTextNode(QString::number(Energy));

            root.appendChild(product);
            product.appendChild(protein);
            protein.appendChild(proteinText);
            product.appendChild(fat);
            fat.appendChild(fatText);
            product.appendChild(carbohydrate);
            carbohydrate.appendChild(carbohydrateText);
            product.appendChild(energy);
            energy.appendChild(energyText);
            if (file1.open(QIODevice::ReadWrite))
            {
            QTextStream out(&file1);
            docProd.save(out, Indent);
            qDebug()<<"File1";
        }
        }
        file.close();
        file1.close();
        file.remove();
        file1.rename("e:/bridget/baseProduct.xml");

    }
}
